package com.szy.news.activity;

import static cn.sharesdk.framework.utils.R.getStringRes;

import java.io.File;
import java.io.FileOutputStream;
import java.io.Serializable;
import java.util.ArrayList;
import java.util.HashMap;

import cn.sharesdk.framework.Platform;
import cn.sharesdk.framework.ShareSDK;
import cn.sharesdk.framework.Platform.ShareParams;
import cn.sharesdk.framework.utils.UIHandler;
import cn.sharesdk.onekeyshare.OnekeyShare;
import cn.sharesdk.onekeyshare.ShareContentCustomizeCallback;
import cn.sharesdk.socialization.Comment;
import cn.sharesdk.socialization.CommentFilter;
import cn.sharesdk.socialization.CommentListener;
import cn.sharesdk.socialization.LikeListener;
import cn.sharesdk.socialization.QuickCommentBar;
import cn.sharesdk.socialization.Socialization;
import cn.sharesdk.socialization.CommentFilter.FilterItem;
import cn.sharesdk.socialization.component.ReplyTooFrequentlyException;
import cn.sharesdk.socialization.component.TopicTitle;
import android.app.Activity;
import android.content.Context;
import android.content.Intent;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.graphics.Bitmap.CompressFormat;
import android.os.Bundle;
import android.os.Handler.Callback;
import android.os.Message;
import android.text.TextUtils;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.TextView;
import android.widget.Toast;

/** 
 * @author 辛晓博
 * @version 创建时间：2014年12月5日 上午10:37:20 
 * 类说明 
 */

public class CommentActivity extends Activity implements Callback, OnClickListener {
	
	private Context context;
	private ArrayList<HashMap<String, Object>> mNewsData;
	private int mNid;
	private String topicId;
	private String topicTitle;
	private OnekeyShare oks;
	private String testImage;
	private QuickCommentBar qcBar;
	private CommentFilter filter;
	private static final String FILE_NAME = "/pic_glance_back.jpg";
	
	private static final int INIT_SDK = 1;
	private static final int AFTER_LIKE = 2;
	
	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		context = CommentActivity.this;
		ShareSDK.initSDK(this);//初始化sharesdk
		ShareSDK.registerService(Socialization.class);//注册社会化组建服务，此方法必须在包含评论与赞控件的UI初始化之前调用
		setContentView(R.layout.newsdetails);
		
		
		new Thread() {
			public void run() {
				initImagePath();
				UIHandler.sendEmptyMessageDelayed(INIT_SDK, 100, CommentActivity.this);
			}
		}.start();
		
		
		/******************拷贝***************************/
		//设置评论监听
		Socialization.setCommentListener(new CommentListener() {

			@Override
			public void onSuccess(Comment comment) {
				int resId = getStringRes(context, "ssdk_socialization_reply_succeeded");
				if (resId > 0) {
					Toast.makeText(context, resId, Toast.LENGTH_SHORT).show();
				}
			}

			@Override
			public void onFail(Comment comment) {
				Toast.makeText(context, comment.getFileCodeString(context), Toast.LENGTH_SHORT).show();
			}

			@Override
			public void onError(Throwable throwable) {
				if (throwable instanceof ReplyTooFrequentlyException) {
					int resId = getStringRes(context, "ssdk_socialization_replay_too_frequently");
					if (resId > 0) {
						Toast.makeText(context, resId, Toast.LENGTH_SHORT).show();
					}
				} else {
					throwable.printStackTrace();
				}
			}
		});

		Socialization.setLikeListener(new LikeListener() {

			@Override
			public void onSuccess(String topicId, String topicTitle, String commentId) {
				Message msg = new Message();
				msg.what = AFTER_LIKE;
				msg.arg1 = 1;
				UIHandler.sendMessage(msg, CommentActivity.this);
			}

			@Override
			public void onFail(String topicId, String topicTitle, String commentId, String error) {
				Message msg = new Message();
				msg.what = AFTER_LIKE;
				msg.arg1 = 2;
				UIHandler.sendMessage(msg, CommentActivity.this);
			}

		});
		
		
		
	}

	
	private void initImagePath() {
		try {
			String cachePath = cn.sharesdk.framework.utils.R.getCachePath(this, null);
			testImage = cachePath + FILE_NAME;
			File file = new File(testImage);
			if (!file.exists()) {
				file.createNewFile();
				Bitmap pic = BitmapFactory.decodeResource(getResources(), R.drawable.pic);
				FileOutputStream fos = new FileOutputStream(file);
				pic.compress(CompressFormat.JPEG, 100, fos);
				fos.flush();
				fos.close();
			}
		} catch(Throwable t) {
			t.printStackTrace();
			testImage = null;
		}
	}
	
	@Override
	public boolean handleMessage(Message msg) {
		
		switch (msg.what) {
		case INIT_SDK:
			topicId = getString(R.string.comment_like_id);
			topicTitle = getString(R.string.comment_like_title);
//			TopicTitle tt = (TopicTitle) findViewById(R.id.llTopicTitle);
//			String topicTitle = getString(R.string.comment_like_title);
//			tt.setTitle(topicTitle);
			/**tt.setPublishTime(getString(R.string.comment_like_publich_time));
			tt.setAuthor(getString(R.string.comment_like_author));
*/
			Socialization service = ShareSDK.getService(Socialization.class);
			service.setCustomPlatform(new MyPlatform(this));
			initOnekeyShare();
//			showOnekeyshare();
			initQuickCommentBar();
			break;
		case AFTER_LIKE:
			if(msg.arg1 == 1){
				//success
				int resId = getStringRes(context, "like_success");
				if (resId > 0) {
					Toast.makeText(context, resId, Toast.LENGTH_SHORT).show();
				}
			}else {
				//fail
				int resId = getStringRes(context, "like_fail");
				if (resId > 0) {
					Toast.makeText(context, resId, Toast.LENGTH_SHORT).show();
				}
			}
			break;
		case 3:
			break;
		default:
			break;

		}
		
		return false;
	}
	
	
	/*******************************拷贝*************************************/
	// Socialization服务依赖OnekeyShare组件，此方法初始化一个OnekeyShare对象
	// 此方法的代码从DemoPage中复制而来
	private void initOnekeyShare() {
		oks = new OnekeyShare();
		oks.setNotification(R.drawable.ic_launcher, getString(R.string.app_name));
		oks.setAddress("12345678901");
		oks.setTitle(getString(R.string.share));
		oks.setTitleUrl("http://mob.com");
		oks.setImagePath(testImage);
		oks.setImageUrl("http://img.appgo.cn/imgs/sharesdk/content/2013/07/25/1374723172663.jpg");
		oks.setUrl("http://www.mob.com");
		oks.setFilePath(testImage);
		oks.setComment(getString(R.string.share));
		oks.setSite(getString(R.string.app_name));
		oks.setSiteUrl("http://mob.com");
		oks.setVenueName("ShareSDK");
		oks.setVenueDescription("This is a beautiful place!");
		oks.setLatitude(23.056081f);
		oks.setLongitude(113.385708f);
		oks.disableSSOWhenAuthorize();
		oks.setShareContentCustomizeCallback(new ShareContentCustomizeCallback() {
			public void onShare(Platform platform, ShareParams paramsToShare) {
					
			}
		});
	}
		
		
//	private  void showOnekeyshare ()  {
//		
//	         
//	    // 分享时Notification的图标和文字 
//	    oks.setNotification (R.drawable.ic_launcher,  
//	    getString (R. string. app_name ) ) ; 
//	    // title标题，印象笔记、邮箱、信息、微信、人人网和QQ空间使用 
//	    oks.setTitle (getString (R. string. share ) ) ; 
//	    // titleUrl是标题的网络链接，仅在人人网和QQ空间使用 
//	    oks.setTitleUrl ( "http://sharesdk.cn" ) ; 
//	    // text是分享文本，所有平台都需要这个字段 
//	    oks.setText (getString (R. string. share ) ) ; 
//	    // imageUrl是图片的网络路径，新浪微博、人人网、QQ空间、 
//	    // 微信的两个平台、Linked-In支持此字段 
//	    oks.setImageUrl ( "http://img.appgo.cn/imgs/sharesdk/content/2013/07/25/1374723172663.jpg" ) ; 
//	    // comment是我对这条分享的评论，仅在人人网和QQ空间使用 
//	    oks.setComment (getString (R. string. share ) ) ; 
//	    // latitude是维度数据，仅在新浪微博、腾讯微博和Foursquare使用 
//	    oks.setLatitude (23.122619f ) ; 
//	    // longitude是经度数据，仅在新浪微博、腾讯微博和Foursquare使用 
//	    oks.setLongitude (113.372338f ) ; 
//	}
		
		
	private void initQuickCommentBar() {
		qcBar = (QuickCommentBar) findViewById(R.id.qcBar);
		qcBar.setTopic(topicId, topicTitle, null, null);
		qcBar.getBackButton().setOnClickListener(this);
		qcBar.setAuthedAccountChangeable(false);
		qcBar.setOnekeyShare(oks);

		CommentFilter.Builder builder = new CommentFilter.Builder();
		// 非空过滤器
		builder.append(new FilterItem() {
			// 返回true表示是垃圾评论
			public boolean onFilter(String comment) {
				if (TextUtils.isEmpty(comment)) {
					return true;
				} else if (comment.trim().length() <= 0) {
					return true;
				} else if (comment.trim().toLowerCase().equals("null")) {
					return true;
				}
				return false;
			}
				@Override
			public int getFilterCode() {
				return 0;
			}
		});
		// 字数上限过滤器
		builder.append(new FilterItem() {
			// 返回true表示是垃圾评论
			public boolean onFilter(String comment) {
				if (comment != null) {
					String pureComment = comment.trim();
					String wordText = cn.sharesdk.framework.utils.R.toWordText(pureComment, 140);
					if (wordText.length() != pureComment.length()) {
						return true;
					}
				}
				return false;
			}
				@Override
			public int getFilterCode() {
				return 0;
			}
		});
		filter = builder.build();
		qcBar.setCommentFilter(filter);
		qcBar.setOnekeyShare(oks);
	}

	@Override
	public void onClick(View v) {
		if (v.equals(qcBar.getBackButton())) {
			finish();
		}
	}
	
	
}
